From 0a85a374e067adb9c65c2a3e5790aad972d6e2b7 Mon Sep 17 00:00:00 2001 From: Ewan Mellor Date: Wed, 13 Dec 2006 11:23:01 +0000 Subject: [PATCH] Steal the write_pidfile function from xenstored_core, and use it to ensure that blktapctrl only starts once. Signed-off-by: Ewan Mellor --- tools/blktap/drivers/blktapctrl.c | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tools/blktap/drivers/blktapctrl.c b/tools/blktap/drivers/blktapctrl.c index cb2258af42..b16ab516b5 100644 --- a/tools/blktap/drivers/blktapctrl.c +++ b/tools/blktap/drivers/blktapctrl.c @@ -57,6 +57,8 @@ #include "blktapctrl.h" #include "tapdisk.h" +#define PIDFILE "/var/run/blktapctrl.pid" + #define NUM_POLL_FDS 2 #define MSG_SIZE 4096 #define MAX_TIMEOUT 10 @@ -622,6 +624,29 @@ static void print_drivers(void) DPRINTF("Found driver: [%s]\n",dtypes[i]->name); } +static void write_pidfile(long pid) +{ + char buf[100]; + int len; + int fd; + + fd = open(PIDFILE, O_RDWR | O_CREAT, 0600); + if (fd == -1) { + DPRINTF("Opening pid file failed (%d)\n", errno); + exit(1); + } + + /* We exit silently if daemon already running. */ + if (lockf(fd, F_TLOCK, 0) == -1) + exit(0); + + len = sprintf(buf, "%ld\n", pid); + if (write(fd, buf, len) != len) { + DPRINTF("Writing pid file failed (%d)\n", errno); + exit(1); + } +} + int main(int argc, char *argv[]) { char *devname; @@ -681,6 +706,7 @@ int main(int argc, char *argv[]) ioctl(ctlfd, BLKTAP_IOCTL_SETMODE, BLKTAP_MODE_INTERPOSE ); process = getpid(); + write_pidfile(process); ret = ioctl(ctlfd, BLKTAP_IOCTL_SENDPID, process ); /*Static pollhooks*/ @@ -716,3 +742,13 @@ int main(int argc, char *argv[]) closelog(); return -1; } + +/* + * Local variables: + * c-file-style: "linux" + * indent-tabs-mode: t + * c-indent-level: 8 + * c-basic-offset: 8 + * tab-width: 8 + * End: + */ -- 2.30.2